package com.lxw.report.meta.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * mybatis 对于插入与更新时的额外字段值填充
 */
@Configuration
public class MybatisPlusMetaHandler implements MetaObjectHandler {

    /**
     * 插入元对象字段填充（用于插入时对公共字段的填充）
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"creationTime", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"rowVersion",Long.class,0L);
        this.strictInsertFill(metaObject,"deleted",Boolean.class,false);
    }

    /**
     * 更新元对象字段填充（用于更新时对公共字段的填充）
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"lastUpdateTime", LocalDateTime.class,LocalDateTime.now());
    }
}
